<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/20
 * Time: 10:07
 */

namespace app\common\mysic\bonus;


use app\common\model\divide\ReceiveUser;
use app\common\model\Order;
use app\common\mysic\MysicBase;
use think\Exception;
use think\Model;

class BonusReceiveUserOp extends MysicBase implements BonusReceiveOpInterface
{
    /**
     * @var Model
     */
    protected $model;
    protected $receiveEntity;
    protected $refundData;
    protected $bonusSetting;
    protected $refundType;
    protected $bonus;

    protected $otherOrder;

    public function setModel($model)
    {
        if (!$model instanceof ReceiveUser || $model == null) {
            throw new Exception('model参数为空 或 非app\common\model\divide\ReceiveUser实例');
        }
        $this->model = $model;
        return $this;
    }
    public function setOtherOrder($otherOrder)
    {
        $this->otherOrder = $otherOrder;

        return $this;
    }
    public function setBonusSetting(Model $setting)
    {
        $this->bonusSetting = $setting;
        return $this;
    }
    public function setReceiveEntity($entity)
    {
        $this->receiveEntity = $entity;
        return $this;
    }
    public function setRefundData($data)
    {
        $this->refundData = $data;
        return $this;
    }
    public function setRefundType(int $type)
    {
        $this->refundType = $type;
        return $this;
    }
    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }
    public function create()
    {
        $this->model->receives_user_id = null;

        $this->model->receives_id = $this->receiveEntity->receives_id;
        $this->model->extend = json_encode([
            [
                'type' => $this->refundType,
                'refund_id' => $this->refundData['refund_id'],
                'refund_fee' => $this->bonus,
                'money' => $this->receiveEntity->payment,
                'order_no' => $this->receiveEntity->order_no,
                'times' => 1,
                'status' => 1,//1 退款成功

                'time' => \time()
            ]
        ]);
        $this->model->create_time = \time();
        $this->model->isUpdate(false);
        $ret = $this->model->save();
        if(!$ret) {
            return false;
        }
        return true;
    }
    public function append()
    {
        $this->log->info('主表ID: ' . $this->receiveEntity->toArray()['receives_id'],'追加分红记录副表开始');
        $receiverUserEntity = $this->model->where('receives_id','=',$this->receiveEntity->receives_id)->find();
        $originExtend = [];
        if($receiverUserEntity){
            $originExtendJson = $receiverUserEntity->extend;
            $this->log->info($originExtendJson ,'追加分红记录副表 - 原始JSON');
            $originExtend = json_decode($originExtendJson, true);
            $originExtend =  is_array($originExtend) ? $originExtend : [];
        }

        $times = count($originExtend) + 1;
        $extend = [
            'type' => $this->refundType,
            'refund_id' => $this->refundData['refund_id'] ?? 0,
            'refund_fee' => $this->bonus,
            'money' => $this->otherOrder->pay_price ?? 0,
            'order_no' => $this->otherOrder->order_no ?? '',
            'times' => $times,
            'status' => 1,//1 退款成功
            'time' => \time()
        ];
        $originExtend[] = $extend;
        $this->log->info(json_encode($originExtend) ,'追加分红记录副表JSON - 新JSON');

        if($receiverUserEntity) {
            $receiverUserEntity->extend = json_encode($originExtend);
            $ret = $receiverUserEntity->isUpdate(true)->save();
        } else {
            $this->model->receives_user_id = null;
            $data = [
                'receives_id' => $this->receiveEntity->receives_id,
                'extend' => json_encode($originExtend),
                'create_time' => time(),
                'update_time' => time()
            ];
            $ret = $this->model->isUpdate(false)->save($data);
        }

        if(!$ret) {
            return false;
        }
        return true;
    }
    public function update($status)
    {
        //todo
    }
}